package com.gzhryc.shared_device.oem.h5.mng.controllers.market.account;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EPayType;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccountDepositOrder;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountDepositOrderService;
import com.gzhryc.net_marketing.cloud.account.services.dto.CloudPayAccountDepositOrderSearch;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class CloudPayAccountDepositOrderController {

    @Route(value = { "/account/deposit_order/list.html" })
    public String list(HttpServletRequest request) {
        return "/account/deposit_order/list.html";
    }

    @Route(value = { "/account/deposit_order/list.data" })
    public ListResult<CloudPayAccountDepositOrder> listData(HttpServletRequest request) {
        ListResult<CloudPayAccountDepositOrder> result = new ListResult<CloudPayAccountDepositOrder>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountDepositOrderSearch search = WebUtils.getParameter(request, CloudPayAccountDepositOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CloudPayAccountDepositOrderService.self().countBySearch(search));
        result.getBody().setData(CloudPayAccountDepositOrderService.self().findBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }

    @Route(value = { "/account/deposit_order/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        CloudPayAccountDepositOrder order = CloudPayAccountDepositOrderService.self().getJoinByTradeNo(tradeNo);
        WebUtils.setAttribute(request,order);
        return "/account/deposit_order/info.html";
    }

    @Route(value = { "/account/deposit_order/stata/syn.do" })
    public AjaxResult doStateSyn(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "同步订单状态失败");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        if (StringTools.isNotBlank(tradeNo)) {
            CloudPayAccountDepositOrder order = CloudPayAccountDepositOrderService.self().getJoinByTradeNo(tradeNo);
            if (order != null) {
                if (EPayType.WXPay.index().equals(order.getPayType())) {
                    try {
                        if (CloudPayAccountDepositOrderService.self().synExternalPayOrder(order)) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        result.setMsg(e.getMessage());
                    }
                } else {
                    result.setMsg("不支持的支付类型");
                }
            }else {
                result.setMsg("未找到支付订单");
            }
        }
        return result;
    }

    @Route(value = { "/account/deposit_order/refund.do" })
    public AjaxResult doRefund(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "退款失败");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        if (StringTools.isNotBlank(tradeNo)) {
            CloudPayAccountDepositOrder order = CloudPayAccountDepositOrderService.self().getJoinByTradeNo(tradeNo);
            if (order != null) {
                if (EPayType.WXPay.index().equals(order.getPayType())) {
                    try {
                        if (CloudPayAccountDepositOrderService.self().refund(order)) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        result.setMsg(e.getMessage());
                    }
                } else {
                    result.setMsg("不支持的支付类型");
                }
            }else {
                result.setMsg("未找到支付订单");
            }
        }
        return result;
    }

    @Route(value = { "/account/deposit_order/del.do" })
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        String tradeNo = WebUtils.getString(request,"tradeNo");
        try {
            if(CloudPayAccountDepositOrderService.self().deleteByTradeNo(tradeNo)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
